<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{padding: 0;margin: 0;}
        .phone{
            width: 300px;
            height: 500px;
            border:1px solid #333;
            margin: 50px auto;
            position: relative;
        }
        .slideBar{
            width: 100%;
            height: 50px;
            background-color: grey;
            position: absolute;
            top: 60%;
            left: 0;
        }
        .ball{
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: pink;
            position: absolute;
            /* left: 50px; */
            left: 0px;
        }
    </style>
</head>
<body>
    <div class="phone">
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>
    // 手机滑动解锁
    // 1. 拖拽小球在slideBar上移动(相对于按下的位置移动)
    // 2. 如果移动过程中到达最右边 直接解锁

    var phone = document.querySelector(".phone");
    var slideBar = document.querySelector(".slideBar");
    var ball = document.querySelector(".ball");

    var maxLeft = phone.clientWidth - ball.clientWidth;

    ball.onmousedown = function(e){
        var e = e || window.event;
        
        var posX = e.offsetX;
       
        document.onmousemove = function(e){
            var e = e || window.event;

            var x = e.pageX - phone.offsetLeft - posX;

            if(x < 0) x = 0;
            if(x > maxLeft) x = maxLeft;

            ball.style.left = x + "px";

            if(x >= maxLeft ){ // 直接移动到最右边  -> 直接解锁
                document.onmousemove =  null;
                location.href = "https://www.baidu.com";
            }

        }

        document.onmouseup = function(e){
            document.onmousemove = null;

            if(ball.offsetLeft < maxLeft){ // 松开时如果没有到达最右边 => 回调原位置
                ball.style.left = 0;
            }

        }

    }




</script>
</html>